// components/user-selector/index.ts
import {pageUser} from '../../api/api'
import { LENGYU_SUCCESS_CODE, PageRequestParam } from '../../utils/request';
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: { // 属性名
      type: Boolean,
      value: false
    },
  },
  observers:{
    show:function(value){
      if(value){
        this.onRefresh();
      }

    },
    initValue:function(){
    }
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {

     },
    hide: function () { },
    resize: function () { 

    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    pageParam:{
      pageIndex:1,
      pageSize:10,
      paramMapObj:{
        vx_nick_name:""
      }
    },
    searchOver:false,
    refresherTriggered:false,
    userList:[
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    closeDialog(){
      this.setData(
        {
          'pageParam.paramMapObj.vx_nick_name':'',
          'userList':[]
        }
      )
      this.setData({
          'show':false
      })
    },
     /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    loadMore() {
      console.log("上拉加载")
      this.getPageList(false);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onRefresh() {
      // console.log("下拉刷新")
      // console.log(this.data.refresherTriggered);
      this.setData({
        'pageParam.pageIndex':1
      })
      this.getPageList(true);
      setTimeout(()=>{
        this.setData({
          'refresherTriggered':false
        })
      },1000)
    
      // console.log(this.data.refresherTriggered);
    },

    async getPageList(refresh:boolean){
      if(!refresh&& this.data.searchOver){
        console.log("已经查询完成不在继续查询")
        return;
      }
      let result = await pageUser(this.data.pageParam as PageRequestParam);
      // console.log(result);
      if(result.data.records.size > this.data.pageParam.pageSize){
        this.setData({
          'pageParam.pageIndex':this.data.pageParam.pageIndex+1
        })
      }
      else{
        this.setData({
          'searchOver':true
        })
      }
      if(refresh){
        this.setData({
          'userList':result.data.records
        })
      }
      else{
     
        this.setData({
          'userList':this.data.userList.concat(result.data.records)
        })
      }
      // console.log("")
    },
    checkboxChange(e:any){
      // console.log("用户选择",e.detail.value)
      let selectUser = this.data.userList[e.detail.value]

      //提交数据到父页面
      this.triggerEvent('selectorChanged', {value:selectUser}) 
      this.closeDialog();
    },
    clearData(){
      this.setData(
        {
          'pageParam.paramMapObj.vx_nick_name':''
        }
      )
    },
    searchChanged(e:any){
     
      this.setData(
        {
          'pageParam.paramMapObj.vx_nick_name':e.detail.value
        }
      )
      this.onRefresh();
    },
    imageError(e:any){
      console.log("图片加载失败",e)

    }

  }
})